/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.libs.LibEvent;

import fever.display.text.AntiAliasType;
import fever.install.InstallUIStrategy;

import flash.filters.DropShadowFilter;

/**
 * Basic UI for preloading system.
 *
 * @see fever.install.DefaultInstaller
 * @see fever.install.FeverInstaller
 * 
 * @author Romain Ecarnot
 */
class fever.install.InstallerUI implements InstallUIStrategy
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _ui : MovieClip;
	private var _ground : MovieClip;
	private var _bar : MovieClip;
	private var _tx : TextField;
	
	private var _backColor : Number;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function InstallerUI( bg : Number )
	{
		_backColor = ( bg == undefined ) ? 0x68808c : bg;
	}
	
	/**
	 * Creates UI.
	 * 
	 * <p>Call by the {@link fever.install.FeverInstaller} preloader instance.
	 */
	public function create( target : MovieClip ) : Void
	{
		_ground = target.createEmptyMovieClip( '__gr__', 2 );
		_ui = target.createEmptyMovieClip( '__ui__', 3 );
		
		var back : MovieClip = _ui.createEmptyMovieClip( '__back__', 3 );
		back.lineStyle( 2, 0x6F7777, 100, true, 'none', 'round', 'round' );
		back.beginFill( 0xffffff, 100 );
		_drawRect( back, 200, 20 );
		back.endFill();
		
		_tx = _ui.createTextField( "__l__", 6, 0, 2, 200, 20 );
		_tx.antiAliasType = AntiAliasType.ADVANCED;
		_tx.selectable = false;
		
		var tf : TextFormat = new TextFormat( '_sans', 10, 0x333333 );
		tf.align = "center";
		_tx.setNewTextFormat( tf );
		
		var filter : DropShadowFilter = new DropShadowFilter( 1, 0, 0x000000, 15, 15, 15, 0.25 );
		_ui.filters = new Array( filter );
		
		Stage.addListener( this );
		onResize();
	}
	
	/**
	 * Process {@code onProgress} event result
	 */
	public function onProgress( e : LibEvent ) : Void
	{
		var p : Number = e.getPerCent();
		
		if( p < 90 )
		{
			if( p < 1 ) _tx.text = 'Waiting';
			else _tx.text = p + " %";
			_bar._xscale = p;
		}
		else
		{
			_bar._xscale = 100;
			_tx.text = 'initialisation';	
		}
	}
	
	/**
	 * Process {@code onTimeOut} event result
	 */
	public function onTimeOut( e : LibEvent ) : Void
	{
		_bar._xscale = 0;
		_tx.text = 'Timeout';	
	}
	
	/**
	 * Disposes UI.
	 */
	public function dispose() : Void
	{
		Stage.removeListener( this );
		
		_ground.removeMovieClip();
		_ui.removeMovieClip();
	}
	
	/**
	 * Returns instance string representation.
	 */
	public function toString() : String
	{
		return 'InstallerUI';	
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Triggered by the Stage.
	 */
	private function onResize() : Void
	{
		var w : Number = Stage.width /2;
		var h : Number = Stage.height /2;
		
		_ui._x = w - _ui._width /2;
		_ui._y = h - _ui._height /2;
		
		_ground.beginFill( _backColor, 100 );
		_drawRect( _ground, Stage.width, Stage.height );
		_ground.endFill();
	}
	
	/**
	 * Simple drawing rect method.
	 */
	private function _drawRect( target : MovieClip, w : Number, h : Number ) : Void
	{
		target.moveTo( 0, 0 );
		target.lineTo( w, 0 );
		target.lineTo( w, h );
		target.lineTo( 0, h );
		target.lineTo( 0, 0 );	
	}	
}